<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="140px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-form-item :label="i18n('organization.logo')" prop="logo">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handleLogoSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.logo" :src="dataForm.logo" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                    </el-upload>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item :label="i18n('organization.tax_code')" prop="tax_code">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.tax_code')" v-model="dataForm.tax_code" show-word-limit maxlength="32" clearable></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.chinese_title')" prop="chinese_title">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.chinese_title')" v-model="dataForm.chinese_title" show-word-limit maxlength="32" clearable></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.english_title')" prop="english_title">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.english_title')" v-model="dataForm.english_title" show-word-limit maxlength="64" clearable></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.register_limit')" prop="register_limit">
                    <el-input-number v-model="dataForm.register_limit" :min="1"/>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.valid_time')" prop="valid_time">
                    <el-date-picker v-model="dataForm.valid_time" type="date" :disabled-date="disabledDate" :placeholder="i18n('common.please_input') + i18n('organization.valid_time')" clearable/>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.inquiry_valid_time')" prop="inquiry_valid_time">
                    <el-date-picker v-model="dataForm.inquiry_valid_time" type="date" :disabled-date="disabledDate" :placeholder="i18n('common.please_input') + i18n('organization.inquiry_valid_time')" clearable/>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.detail_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item :label="i18n('organization.archive.chinese_description')" prop="chinese_description">
                <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('organization.archive.chinese_description')" v-model="dataForm.chinese_description" rows="4" show-word-limit maxlength="500"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('organization.archive.english_description')" prop="english_description">
                <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('organization.archive.english_description')" v-model="dataForm.english_description" rows="4" show-word-limit maxlength="500"></el-input>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.archive.info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="3">
                  <el-form-item :label="i18n('organization.archive.web_background_url')" prop="web_background_url">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handleWebSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.web_background_url" :src="dataForm.web_background_url" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                    </el-upload>
                  </el-form-item>
                </el-col>

                <el-col :span="3">
                  <el-form-item :label="i18n('organization.archive.wechat_background_url')" prop="wechat_background_url">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handleWechatSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.wechat_background_url" :src="dataForm.wechat_background_url" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                    </el-upload>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:organization:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization')

  const current_time = ref('')

  const dataForm = reactive({
    id: 0,
    logo: '',
    tax_code: '',
    chinese_title: '',
    english_title: '',
    register_limit: 1,
    valid_time: '',
    inquiry_valid_time: '',
    chinese_description: '',
    english_description: '',
    web_background_url: '',
    wechat_background_url: '',
    inquiry_publish: '1',
    schedule_publish: '2',
    supplier_status: '2',
  })

  const dataRule = reactive({
    logo: [{
      required: true,
      message: i18n('organization.rules.logo.require'),
      trigger: 'blur',
    }],
    chinese_title: [{
      required: true,
      message: i18n('organization.rules.chinese_title.require'),
      trigger: 'blur',
    }],
    english_title: [{
      required: true,
      message: i18n('organization.rules.english_title.require'),
      trigger: 'blur',
    }],
    register_limit: [{
      required: true,
      message: i18n('organization.rules.register_limit.require'),
      trigger: 'blur',
    }],
    valid_time: [{
      required: true,
      message: i18n('organization.rules.valid_time.require'),
      trigger: 'blur',
    }],
    inquiry_valid_time: [{
      required: true,
      message: i18n('organization.rules.inquiry_valid_time.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handleLogoSuccess,
    handleBusinessLicenseSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.logo = data.data.logo
            dataForm.tax_code = data.data.tax_code
            dataForm.chinese_title = data.data.chinese_title
            dataForm.english_title = data.data.english_title
            dataForm.register_limit = data.data.register_limit
            dataForm.valid_time = data.data.valid_time
            dataForm.inquiry_valid_time = data.data.inquiry_valid_time
            dataForm.inquiry_publish = data.data.inquiry_publish.value
            dataForm.schedule_publish = data.data.schedule_publish.value
            dataForm.supplier_status = data.data.supplier_status.value
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/archive/data`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.chinese_description = data.data.chinese_description
                  dataForm.english_description = data.data.english_description
                  dataForm.web_background_url = data.data.web_background_url
                  dataForm.wechat_background_url = data.data.wechat_background_url
                }
              }
            })
          }
        })
      }
    })
  }

  const handleWebSuccess = (res, file) => {
    dataForm.web_background_url = res.data
  }

  const handleWechatSuccess = (res, file) => {
    dataForm.wechat_background_url = res.data
  }

  const disabledDate = (time) => {
    return time.getTime() - 24 * 60 * 60 * 1000 * 30 < current_time.value
  }

  onMounted(() => {
    current_time.value = Date.now()

    init(formRef)
  })
</script>
